/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.rest;

import co.yixiang.api.YshopException;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.protocol.domain.Protocol;
import co.yixiang.modules.protocol.service.ProtocolService;
import co.yixiang.modules.protocol.service.dto.ProtocolDto;
import co.yixiang.modules.protocol.service.dto.ProtocolQueryCriteria;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
* @author hupeng
* @date 2020-09-09
*/
@AllArgsConstructor
@Api(tags = "protocol管理")
@RestController
@RequestMapping("/api/yxProtocol")
public class YxProtocolController {

    private final ProtocolService yxProtocolService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','yxProtocol:list')")
    public void download(HttpServletResponse response, ProtocolQueryCriteria criteria) throws IOException {
        yxProtocolService.download(generator.convert(yxProtocolService.queryAll(criteria), ProtocolDto.class), response);
    }

    @GetMapping
    @Log("查询protocol")
    @ApiOperation("查询protocol")
    @PreAuthorize("@el.check('admin','yxProtocol:list')")
    public ResponseEntity<Object> getYxProtocols(ProtocolQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxProtocolService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增protocol")
    @ApiOperation("新增protocol")
    @PreAuthorize("@el.check('admin','yxProtocol:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Protocol resources){
        //判断当前类型是否存在
        int count = yxProtocolService.count(new QueryWrapper<Protocol>().lambda().eq(Protocol::getProtocolType, resources.getProtocolType()));

        if(count != 0){
            throw new YshopException("当前类型已经存在！");
        }

        return new ResponseEntity<>(yxProtocolService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改protocol")
    @ApiOperation("修改protocol")
    @PreAuthorize("@el.check('admin','yxProtocol:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Protocol resources){
        //判断当前类型是否存在
        int count = yxProtocolService.count(new QueryWrapper<Protocol>().lambda().eq(Protocol::getProtocolType, resources.getProtocolType()));
        int count1 = yxProtocolService.count(new QueryWrapper<Protocol>().lambda()
                .eq(Protocol::getProtocolType, resources.getProtocolType())
                .eq(Protocol::getId ,resources.getId()));

        if(count != 0 && count1 != 1){
            throw new YshopException("当前类型已经存在！");
        }

        yxProtocolService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除protocol")
    @ApiOperation("删除protocol")
    @PreAuthorize("@el.check('admin','yxProtocol:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody String[] ids) {
        Arrays.asList(ids).forEach(id->{
            yxProtocolService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
